/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 13/05/2011
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

import java.io.DataOutputStream;
import java.io.OutputStream;

/**
 * The PDBOutputStream class manage a output stream of datas
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBOutputStream extends DataOutputStream {
	private byte[] content;

	// Constantes
	public static final byte[] ZERO = {0};

	/**
	 * Constructor
	 *
	 * @param is	Stream to read
	 */
	public PDBOutputStream(OutputStream os) {
		super(os);
		content = null;
	}

	/**
	 * Write a numeric value
	 *
	 * @param val	Value to write
	 * @param len	Number of byte to write (1, 2, 4, 8)
	 */
	public void writeValue(long val, int len) throws Exception {
		try {
			content = new byte[len];
			for(int i=1; i<=len; i++) {
				content[len-i] = (byte)(val & 0xFF);
				val = val >> 8;
			}
			write(content);
		}
		catch (Exception e) {
			throw new Exception("PDBOutputStream.writeValue : "+e.getMessage()+" writing "+val+" (len="+len);
		}
	}

	/**
	 * Write a string value
	 *
	 * @param val	Value to write
	 */
	public void writeString(String val) throws Exception {
		try {
			content = val.getBytes();
			write(content);
			write(ZERO);
		}
		catch (Exception e) {
			throw new Exception("PDBOutputStream.writeString : "+e.getMessage()+" writing '"+val+"'");
		}
	}
}